package com.zhiyou.app.shop.website.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;

import com.zhiyou.app.shop.common.BaseAction;
import com.zhiyou.app.shop.common.util.ShopConstants;
import com.zhiyou.app.shop.website.dao.pojo.ActivityApply;
import com.zhiyou.app.shop.website.dao.pojo.DicItem;
import com.zhiyou.app.shop.website.dao.pojo.LoveFound;
import com.zhiyou.app.shop.website.dao.pojo.MoneyDeal;
import com.zhiyou.app.shop.website.dao.pojo.TransferInfo;
import com.zhiyou.app.shop.website.dao.pojo.User;
import com.zhiyou.app.shop.website.dao.pojo.UserAccount;
import com.zhiyou.app.shop.website.dao.pojo.UserBank;
import com.zhiyou.app.shop.website.service.IDicItemService;
import com.zhiyou.app.shop.website.service.IMoneyDealService;
import com.zhiyou.app.shop.website.service.IPlayService;
import com.zhiyou.app.shop.website.service.IUserAccountService;
import com.zhiyou.app.shop.website.service.IUserManagerService;

public class PlayManagerAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserAccountService userAccountService;
	@Autowired
	private IUserManagerService userManagerService;
	@Autowired
	private IPlayService playService;
	@Autowired
	private IDicItemService dicItemService;
	@Autowired
	private IMoneyDealService moneyDealService;

	private UserAccount userAccount;
	private UserBank userBank;
	private List<MoneyDeal> moneyDeal;
	private List<TransferInfo> transferList;
	
	private String exchangeType; // 兑换类型
	private String exchangeAmount; // 兑换金额
	private String thirdPwd;///三级密码
	
	private String receiveAccount;  //接收账户
	private String convertAmount;   //互转金额
	private String convertRate;
	private List converList;  //互转列表
	private int totalCount;
	private Integer totalPage;
	
	private String applyAmount;   //申请激活豆金额
	private String randTwo;     //随机生成的两位小数
	private BigDecimal fectAmount;  //实际回款金额
	private String loveRateP;  //爱心基金百分比
	private String loveRate;   //爱心基金百分比  小数
	
	private String applyId; //申请单id
	private ActivityApply apply;
	
	private List<User> listUser;
	
	private BigDecimal exStockAmount; //咖啡豆转游戏豆最大量
	private BigDecimal stockExchange; //已经转的游戏豆的数量
	
	//兑换中心
	public String exchangePage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		//咖啡豆转游戏豆  不能超过本金的2倍
		DicItem level = dicItemService.findByTypeAndCode(ShopConstants.LEVEL,user.getLevel());//当前用户级别
		DicItem dicItem = dicItemService.findByTypeAndCode(ShopConstants.EXCHANG_STOCK_RATE, "1");
		exStockAmount = new BigDecimal(level.getAttr1()).multiply(new BigDecimal(dicItem.getName()));
		stockExchange = moneyDealService.getStockExchang(user.getUserId());
		return SUCCESS;
	}
	public String exchangeList(){
		pageSize = 5;
		moneyDealService.findExchangeDeal(getInitedPage(), getCurrentUser().getUserId(), "2", "null");
		return SUCCESS;
	}
	public String doExchange(){
		getResponse().setContentType("text/html; charset=UTF-8");
		playService.doExchange(getCurrentUser().getUserId(), new BigDecimal(exchangeAmount), exchangeType);
		try {
			PrintWriter out = getResponse().getWriter();
			out.print("兑换成功!");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//互转中心
	public String conversionPage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		/*DicItem dicItem = null;
		List<DicItem> dicList = dicItemService.findByType(ShopConstants.MUTUAL_CONVERSION);
		if(dicList != null && dicList.size() > 0){
			dicItem = dicList.get(0);
			convertRate = dicItem.getCode();
		}*/
		return SUCCESS;
	}
	public String conversionList(){
		pageSize = 10;
		User user = getCurrentUser();
		//playService.findTransfer(getInitedPage(), getCurrentUser().getUserAccount(), "互转");
		int pageOffset = new Integer(pageNo) != null ? (pageNo-1) * pageSize : 0;
		converList = playService.findTransfer(user.getUserAccount(),  pageOffset, pageSize);
		pageNo = pageOffset/pageSize + 1;
		//epStatusList = dicItemService.findByType(ShopConstants.EP_STATUS);
		totalCount = playService.findTransferCnt(user.getUserAccount());
		totalPage = totalCount%pageSize!=0?totalCount/pageSize+1:totalCount/pageSize;
		return SUCCESS;
	}
	public String doConversion(){
		getResponse().setContentType("text/html; charset=UTF-8");
		User user = getCurrentUser();
		List<User> userList = userManagerService.getUser(receiveAccount);
		try {
			if(userList != null && userList.size() > 0){
				User toUser = userList.get(0);
				playService.doConvert(user.getUserId(), toUser.getUserId(), new BigDecimal(convertAmount));
				PrintWriter out = getResponse().getWriter();
				out.print("互转成功!");
				out.flush();
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//VV账户互转
	public String toVvConvert(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		
		if(user.getMainAccount() == null ||"".equals(user.getMainAccount())){//非附属
			listUser = userManagerService.getVvUser(user.getUserAccount()); //查询附属账户
		}else{ //附属账户
			listUser = userManagerService.getUser(user.getMainAccount());  //查询主账户
		}
		return SUCCESS;
	}
	public String vvConvertList(){
		pageSize = 10;
		playService.findTransfer(getInitedPage(), getCurrentUser().getUserAccount(), "VV账户互转");
		return SUCCESS;
	}
	public String doVvConvert(){
		getResponse().setContentType("text/html; charset=UTF-8");
		try {
			User user = getCurrentUser();
			List<User> userList = userManagerService.getUser(receiveAccount);
			if(userList != null && userList.size() > 0){
				User toUser = userList.get(0);
				playService.doVvconvert(user.getUserId(), toUser.getUserId(), new BigDecimal(convertAmount),exchangeType);
			}
			PrintWriter out = getResponse().getWriter();
			out.print("VV账户互转成功");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//咖啡豆
	public String coffeePage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		pageSize = 10;
		MoneyDeal money = new MoneyDeal();
		money.setUserId(user.getUserId());
		money.setAccountType(ShopConstants.PRIZE);
		playService.getMoneyDeal(getInitedPage(), money);
		return SUCCESS;
	}
	//激活豆
	public String activationPage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		pageSize = 10;
		MoneyDeal money = new MoneyDeal();
		money.setUserId(user.getUserId());
		money.setAccountType(ShopConstants.ACTIVITY);
		playService.getMoneyDeal(getInitedPage(), money);
		return SUCCESS;
	}
	//游戏豆
	public String gamePage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		pageSize = 10;
		MoneyDeal money = new MoneyDeal();
		money.setUserId(user.getUserId());
		money.setAccountType(ShopConstants.STOCK);
		playService.getMoneyDeal(getInitedPage(), money);
		return SUCCESS;
	}

	//仙草豆
	public String xiancaoPage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		pageSize = 10;
		MoneyDeal money = new MoneyDeal();
		money.setUserId(user.getUserId());
		money.setAccountType(ShopConstants.RESUMER);
		playService.getMoneyDeal(getInitedPage(), money);
		return SUCCESS;
	}
	
	//咖啡豆交易明细
	public String detailPage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		pageSize = 10;
		MoneyDeal money = new MoneyDeal();
		money.setUserId(user.getUserId());
		money.setAccountType(ShopConstants.PRIZE);
		playService.getMoneyDeal(getInitedPage(), money);
		return SUCCESS;
	}
	
	//激活豆中心   新增功能   申请激活豆
	public String activityPage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		Random rand = new Random();
		randTwo = 10+ rand.nextInt(89)+"";
		return SUCCESS;
	}
	public String activityList(){
		pageSize = 10;
		playService.findApplyActivity(getInitedPage(), getCurrentUser().getUserAccount(), null);
		return SUCCESS;
	}
	public String doActivity(){
		getResponse().setContentType("text/html; charset=UTF-8");
		try {
			DicItem actrate = new DicItem();
			List<DicItem> dicItems = dicItemService.findByType(ShopConstants.APPLY_ACTIVITY_RATE);
			if(dicItems != null && dicItems.size() > 0){
				actrate = dicItems.get(0);
			}
			ActivityApply apply = new ActivityApply();
			apply.setApplyAmount(new BigDecimal(applyAmount));
			apply.setApplyDate(new Timestamp(new Date().getTime()));
			apply.setCreateDate(apply.getApplyDate());
			apply.setStatus(ShopConstants.TRUE); //0：待审核
			apply.setApplyId(getCurrentUser().getUserAccount());
			apply.setActAmount(new BigDecimal(applyAmount).add(new BigDecimal(applyAmount).multiply(new BigDecimal(actrate.getCode()))));   //实际发放激活豆
			fectAmount = new BigDecimal(applyAmount).add(new BigDecimal(randTwo).divide(new BigDecimal(100)));
			apply.setCash(fectAmount); //实际汇款数  自动为申请报单金额后面随机加一个两位小数
			playService.saveApplyActivi(apply);
			
			userBank = userManagerService.findAdminBank();
			
			PrintWriter out = getResponse().getWriter();
			out.print("申请已提交，请及时汇款，并填写汇款单!");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String toRemit(){
		apply = playService.findById(applyId);
		return SUCCESS;
	}
	
	private String remitBank;
	private String remitCard;
	private String remitCash;
	private String remitDate;
	private String remitPerson;
	public String applyActivity(){
		getResponse().setContentType("text/html; charset=UTF-8");
		try {
			apply = playService.findById(applyId);
			apply.setRemitBank(remitBank);
			apply.setRemitCard(remitCard);
			apply.setRemitCash(new BigDecimal(remitCash));
			apply.setRemitDate( Timestamp.valueOf(remitDate));
			apply.setRemitPerson(remitPerson);
			apply.setStatus("1");  //0:申请   1：已汇款 ，待审核   2：申请成功
			playService.updateApply(apply);
			PrintWriter out = getResponse().getWriter();
			out.print("您的汇款信息已提交，等待客服审核");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getBank(){
		apply = playService.findById(applyId);
		userBank = userManagerService.findAdminBank();
		return SUCCESS;
	}
	
	//爱心基金
	public String lovePage(){
		User user = getCurrentUser();
		userAccount = userAccountService.findUserAccount(user.getUserAccount());
		DicItem dicItem = null;
		List<DicItem> dicList = dicItemService.findByType(ShopConstants.LOVE_FOUND);
		if(dicList != null && dicList.size() > 0){
			dicItem = dicList.get(0);
			loveRateP = dicItem.getRemark();
			loveRate = dicItem.getCode();
		}
		return SUCCESS;
	}
	
	public String loveFound(){
		getResponse().setContentType("text/html; charset=UTF-8");
		try {
			User user = getCurrentUser();
			userAccount = userAccountService.findUserAccount(user.getUserAccount());
			BigDecimal lovefound = new BigDecimal(applyAmount);
			userAccount.setRecommendMoney(userAccount.getRecommendMoney().subtract(lovefound));
			userAccountService.saveAccount(userAccount);
			//BigDecimal lovefound = userAccount.getBonusMoney().multiply(new BigDecimal(loveRate));//爱心基金
			LoveFound love = new LoveFound();
			love.setUserId(user.getUserAccount());
			love.setLoveAmount(lovefound);
			love.setCreateDate(new Timestamp(new Date().getTime()));
			playService.saveLoveFound(love);
			
			PrintWriter out = getResponse().getWriter();
			out.print("申请以提交，请及时汇款，并填写汇款单!");
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	public UserAccount getUserAccount() {
		return userAccount;
	}
	public void setUserAccount(UserAccount userAccount) {
		this.userAccount = userAccount;
	}
	public UserBank getUserBank() {
		return userBank;
	}
	public void setUserBank(UserBank userBank) {
		this.userBank = userBank;
	}
	public List<MoneyDeal> getMoneyDeal() {
		return moneyDeal;
	}
	public void setMoneyDeal(List<MoneyDeal> moneyDeal) {
		this.moneyDeal = moneyDeal;
	}
	public String getExchangeType() {
		return exchangeType;
	}
	public void setExchangeType(String exchangeType) {
		this.exchangeType = exchangeType;
	}
	public String getExchangeAmount() {
		return exchangeAmount;
	}
	public void setExchangeAmount(String exchangeAmount) {
		this.exchangeAmount = exchangeAmount;
	}
	public String getReceiveAccount() {
		return receiveAccount;
	}
	public void setReceiveAccount(String receiveAccount) {
		this.receiveAccount = receiveAccount;
	}
	public String getConvertAmount() {
		return convertAmount;
	}
	public void setConvertAmount(String convertAmount) {
		this.convertAmount = convertAmount;
	}
	public String getConvertRate() {
		return convertRate;
	}
	public void setConvertRate(String convertRate) {
		this.convertRate = convertRate;
	}
	public String getLoveRate() {
		return loveRate;
	}
	public void setLoveRate(String loveRate) {
		this.loveRate = loveRate;
	}
	public String getLoveRateP() {
		return loveRateP;
	}
	public void setLoveRateP(String loveRateP) {
		this.loveRateP = loveRateP;
	}
	public String getApplyAmount() {
		return applyAmount;
	}
	public void setApplyAmount(String applyAmount) {
		this.applyAmount = applyAmount;
	}
	public String getRandTwo() {
		return randTwo;
	}
	public void setRandTwo(String randTwo) {
		this.randTwo = randTwo;
	}
	public BigDecimal getFectAmount() {
		return fectAmount;
	}
	public void setFectAmount(BigDecimal fectAmount) {
		this.fectAmount = fectAmount;
	}
	public String getApplyId() {
		return applyId;
	}
	public void setApplyId(String applyId) {
		this.applyId = applyId;
	}
	public ActivityApply getApply() {
		return apply;
	}
	public void setApply(ActivityApply apply) {
		this.apply = apply;
	}
	public List<TransferInfo> getTransferList() {
		return transferList;
	}
	public void setTransferList(List<TransferInfo> transferList) {
		this.transferList = transferList;
	}
	public String getThirdPwd() {
		return thirdPwd;
	}
	public void setThirdPwd(String thirdPwd) {
		this.thirdPwd = thirdPwd;
	}
	public String getRemitBank() {
		return remitBank;
	}
	public void setRemitBank(String remitBank) {
		this.remitBank = remitBank;
	}
	public String getRemitCard() {
		return remitCard;
	}
	public void setRemitCard(String remitCard) {
		this.remitCard = remitCard;
	}
	public String getRemitCash() {
		return remitCash;
	}
	public void setRemitCash(String remitCash) {
		this.remitCash = remitCash;
	}
	public String getRemitDate() {
		return remitDate;
	}
	public void setRemitDate(String remitDate) {
		this.remitDate = remitDate;
	}
	public String getRemitPerson() {
		return remitPerson;
	}
	public void setRemitPerson(String remitPerson) {
		this.remitPerson = remitPerson;
	}
	public List<User> getListUser() {
		return listUser;
	}
	public void setListUser(List<User> listUser) {
		this.listUser = listUser;
	}
	public BigDecimal getExStockAmount() {
		return exStockAmount;
	}
	public void setExStockAmount(BigDecimal exStockAmount) {
		this.exStockAmount = exStockAmount;
	}
	public List getConverList() {
		return converList;
	}
	public void setConverList(List converList) {
		this.converList = converList;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public BigDecimal getStockExchange() {
		return stockExchange;
	}
	public void setStockExchange(BigDecimal stockExchange) {
		this.stockExchange = stockExchange;
	}
	 
}
